//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "StickyPayoff.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/DoubleStickyRatchetPayoff.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CStickyPayoff::CStickyPayoff (Double gearing1, Double gearing2, Double spread1, Double spread2, Double initialValue, Double accrualFactor) : CDoubleStickyRatchetPayoff(CStickyPayoff::typeid)
{
    try
    {
#ifdef HANDLE
        _phStickyPayoff = NULL;
#endif
        QuantLib::Real _gearing1 = (QuantLib::Real)ValueHelper::Convert (gearing1);
        QuantLib::Real _gearing2 = (QuantLib::Real)ValueHelper::Convert (gearing2);
        QuantLib::Real _spread1 = (QuantLib::Real)ValueHelper::Convert (spread1);
        QuantLib::Real _spread2 = (QuantLib::Real)ValueHelper::Convert (spread2);
        QuantLib::Real _initialValue = (QuantLib::Real)ValueHelper::Convert (initialValue);
        QuantLib::Real _accrualFactor = (QuantLib::Real)ValueHelper::Convert (accrualFactor);
        _ppStickyPayoff = new boost::shared_ptr<QuantLib::StickyPayoff> (new QuantLib::StickyPayoff ( _gearing1,  _gearing2,  _spread1,  _spread2,  _initialValue,  _accrualFactor ));
        SetDoubleStickyRatchetPayoff (boost::dynamic_pointer_cast<QuantLib::DoubleStickyRatchetPayoff> (*_ppStickyPayoff));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CStickyPayoff::CStickyPayoff (boost::shared_ptr<QuantLib::StickyPayoff>& childNative, Object^ owner) : CDoubleStickyRatchetPayoff(CStickyPayoff::typeid)
{
#ifdef HANDLE
	_phStickyPayoff = NULL;
#endif
	_ppStickyPayoff = &childNative;
    _ppDoubleStickyRatchetPayoff = new boost::shared_ptr<QuantLib::DoubleStickyRatchetPayoff> (boost::dynamic_pointer_cast<QuantLib::DoubleStickyRatchetPayoff> (*_ppStickyPayoff));
}
Cephei::QL::Instruments::CStickyPayoff::CStickyPayoff (QuantLib::StickyPayoff& childNative, Object^ owner) : CDoubleStickyRatchetPayoff(CStickyPayoff::typeid)
{
#ifdef HANDLE
	_phStickyPayoff = NULL;
#endif
	_ppStickyPayoff = new boost::shared_ptr<QuantLib::StickyPayoff> (&childNative);
    _ppDoubleStickyRatchetPayoff = new boost::shared_ptr<QuantLib::DoubleStickyRatchetPayoff> (boost::dynamic_pointer_cast<QuantLib::DoubleStickyRatchetPayoff> (*_ppStickyPayoff));
    _StickyPayoffOwner = owner;
    _DoubleStickyRatchetPayoffOwner = owner;
}

Cephei::QL::Instruments::CStickyPayoff::CStickyPayoff (CStickyPayoff^ copy) : CDoubleStickyRatchetPayoff(CStickyPayoff::typeid)
{
#ifdef HANDLE
	_phStickyPayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppStickyPayoff = new boost::shared_ptr<QuantLib::StickyPayoff> (copy->GetShared());
        _ppDoubleStickyRatchetPayoff = new boost::shared_ptr<QuantLib::DoubleStickyRatchetPayoff> (boost::dynamic_pointer_cast<QuantLib::DoubleStickyRatchetPayoff> (*_ppStickyPayoff));
    }
}
Cephei::QL::Instruments::CStickyPayoff::CStickyPayoff (System::Type^ t) : CDoubleStickyRatchetPayoff(CStickyPayoff::typeid)
{
#ifdef HANDLE
	_phStickyPayoff = NULL;
#endif
	if (!t->IsSubclassOf(CStickyPayoff::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CStickyPayoff::CStickyPayoff (QuantLib::Handle<QuantLib::StickyPayoff>& childNative, Object^ owner)  : CDoubleStickyRatchetPayoff(CStickyPayoff::typeid)
{
	_phStickyPayoff = &childNative;
	_ppStickyPayoff = &static_cast<boost::shared_ptr<QuantLib::StickyPayoff>>(childNative.currentLink());
    _ppDoubleStickyRatchetPayoff = new boost::shared_ptr<QuantLib::DoubleStickyRatchetPayoff> (boost::dynamic_pointer_cast<QuantLib::DoubleStickyRatchetPayoff> (*_ppStickyPayoff));
    _StickyPayoffOwner = owner;
}
Cephei::QL::Instruments::CStickyPayoff::CStickyPayoff (QuantLib::Handle<QuantLib::StickyPayoff> childNative)  : CDoubleStickyRatchetPayoff(CStickyPayoff::typeid)
{
	_phStickyPayoff = &childNative;
	_ppStickyPayoff = &static_cast<boost::shared_ptr<QuantLib::StickyPayoff>>(childNative.currentLink());
    _ppDoubleStickyRatchetPayoff = new boost::shared_ptr<QuantLib::DoubleStickyRatchetPayoff> (boost::dynamic_pointer_cast<QuantLib::DoubleStickyRatchetPayoff> (*_ppStickyPayoff));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CStickyPayoff::CStickyPayoff (QuantLib::StickyPayoff childNative)  : CDoubleStickyRatchetPayoff(CStickyPayoff::typeid)
{
#ifdef HANDLE
	_phStickyPayoff = NULL;
#endif
	_ppStickyPayoff = new boost::shared_ptr<QuantLib::StickyPayoff> (new QuantLib::StickyPayoff (childNative));
    _ppDoubleStickyRatchetPayoff = new boost::shared_ptr<QuantLib::DoubleStickyRatchetPayoff> (boost::dynamic_pointer_cast<QuantLib::DoubleStickyRatchetPayoff> (*_ppStickyPayoff));
}
#endif

Cephei::QL::Instruments::CStickyPayoff::~CStickyPayoff ()
{
    if (_ppStickyPayoff != NULL)
    {
	    delete _ppStickyPayoff;
        _ppStickyPayoff = NULL;
    }
}
Cephei::QL::Instruments::CStickyPayoff::!CStickyPayoff ()
{
    if (_ppStickyPayoff != NULL)
    {
	    delete _ppStickyPayoff;
    }
}
QuantLib::StickyPayoff& Cephei::QL::Instruments::CStickyPayoff::GetReference ()
{
    if (_ppStickyPayoff == NULL) throw gcnew NativeNullException ();
	return **_ppStickyPayoff;
}
boost::shared_ptr<QuantLib::StickyPayoff>& Cephei::QL::Instruments::CStickyPayoff::GetShared ()
{
    if (_ppStickyPayoff == NULL) throw gcnew NativeNullException ();
	return *_ppStickyPayoff;
}
QuantLib::StickyPayoff* Cephei::QL::Instruments::CStickyPayoff::GetPointer ()
{
    if (_ppStickyPayoff == NULL) throw gcnew NativeNullException ();
	return &**_ppStickyPayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::StickyPayoff>& Cephei::QL::Instruments::CStickyPayoff::GetHandle ()
{
	if (_phStickyPayoff == NULL)
	{
		_phStickyPayoff = new Handle<QuantLib::StickyPayoff> (*_ppStickyPayoff);
	}
	return *_phStickyPayoff;
}
#endif
bool Cephei::QL::Instruments::CStickyPayoff::HasNative () 
{
	return (_ppStickyPayoff != NULL);
}

String^ Cephei::QL::Instruments::CStickyPayoff::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppStickyPayoff)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IStickyPayoff^ Cephei::QL::Instruments::CStickyPayoff_Factory::Create (Double gearing1, Double gearing2, Double spread1, Double spread2, Double initialValue, Double accrualFactor)
{
    return gcnew CStickyPayoff ( gearing1,  gearing2,  spread1,  spread2,  initialValue,  accrualFactor);
}
